package jc.malpractice.controllers;

import cn.hutool.json.JSONObject;
import jc.malpractice.service.MastService;
import jc.malpractice.service.UserService;
import spark.Route;

import static spark.Spark.get;
import static spark.Spark.post;

public final class UserController {

    static Route Login = (req, res) -> {
        JSONObject jo = UserService.login(req.queryParams("username"),
                req.queryParams("password"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        req.session().attribute("username", jo.get("password"));
        return jo;
    };

    static Route reg = (req, res) -> {
        JSONObject jo = UserService.reg(req.queryParams("username"),
                req.queryParams("password"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        req.session().attribute("username", jo.get("password"));
        return jo;
    };

    static Route Check = (req, res) -> {
        Object pac_ward = req.session().attribute("pac_ward");
        JSONObject jo = new JSONObject();

        if (pac_ward == null) {
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        } else {
            jo.set("code", 0);
            jo.set("data", pac_ward);
            jo.set("msg", "session未失效");
        }

        return jo;
    };

    static Route Logout = (req, res) -> {
        req.session().attribute("pac_ward", null);

        JSONObject jo = new JSONObject();
        jo.set("code", 0);
        jo.set("msg", "登出成功");

        return jo;
    };


    static Route selectUser = (req, res) -> {
        JSONObject jo = UserService.selectUser(req.queryParams("role"),req.queryParams("reporter"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        return jo;
    };

    static Route selectReporter = (req, res) -> {
        JSONObject jo = null;
        if("admin".equals(req.queryParams("username"))&&"123".equals(req.queryParams("password"))) {
            jo = UserService.selectReporter();
        }

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        return jo;
    };

    static Route update_is_admin = (req, res) -> {
        JSONObject jo = UserService.update_is_admin(req.queryParams("id"),req.queryParams("is_admin"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        return jo;
    };

    static Route updatePassword = (req, res) -> {
        JSONObject jo = UserService.updatePassword(req.queryParams("id"),req.queryParams("password"));

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "session失效");
        }

        return jo;
    };

    static Route sel_hander = (req, res) -> {


        return UserService.sel_hander();
    };

    public static void Init() {
        get("/user/login", Login);
        get("/user/reg", reg);
        get("/user/check", Check);
        get("/user/logout", Logout);
        get("/user/selectUser", selectUser);
        get("/user/selectReporter", selectReporter);
        post("/user/updatePassword", updatePassword);
        post("/user/update_is_admin", update_is_admin);
        get("/test/sel_employee",UserController.sel_hander);
    }
}
